package com.shizhanzhe.zxbapp.task;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.bean.Wechat;
import com.shizhanzhe.zxbapp.my.TiXianActivity;
import com.shizhanzhe.zxbapp.ui.LoginActivity;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.widget.ErrorCallback;
import com.shizhanzhe.zxbapp.widget.MyListView;

import java.util.List;

/**
 * 关注
 * Created by zz9527 on 2018/10/19.
 */
public class TabFollowFragment extends Fragment {

    private ListView lv;
    private SmartRefreshLayout refresh;
    private ImageView banner;
    private ImageView nodata;

    public static TabFollowFragment newInstance(int level) {

        Bundle args = new Bundle();
        args.putInt("level", level);
        TabFollowFragment fragment = new TabFollowFragment();
        fragment.setArguments(args);
        return fragment;
    }
    LoadService loadService;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //第一步：获取布局View
        View rootView=View.inflate(getActivity(),R.layout.fragment_tab_follow,  null);
        //第二步：注册布局View
        loadService = LoadSir.getDefault().register(rootView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                // 重新加载逻辑
                getData();
            }
        });
        //第三步：返回LoadSir生成的LoadLayout
        return loadService.getLoadLayout();
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        getData();
    }

    private void initView(View view) {
        nodata = view.findViewById(R.id.nodata);
        lv = view.findViewById(R.id.lv_follow);
        refresh = view.findViewById(R.id.refresh);
        banner = view.findViewById(R.id.iv_follow_banner);
        //设置 Header 样式
        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refresh.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                getData();
            }
        });
    }

    private void getData() {
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.WECHATSUBSCRPTION(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        lv.setVisibility(View.VISIBLE);
                        Gson gson = new Gson();
                        final Wechat wechat = gson.fromJson(json, Wechat.class);
                        final List<Wechat.DataBean> list = wechat.getData();
                        if (list.size()==0){
                            nodata.setVisibility(View.VISIBLE);
                        }
                        lv.setAdapter(new FollowAdapter(list, getContext()));
                        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                if (UserBean.uid == 0) {
                                    startActivity(new Intent(getContext(), LoginActivity.class));
                                } else {
                                    Intent intent = new Intent(getActivity(), FollowActivity.class);
                                    intent.putExtra("iv", list.get(i).getHead());
                                    intent.putExtra("title", list.get(i).getName());
                                    intent.putExtra("one", list.get(i).getDan());
                                    intent.putExtra("all", list.get(i).getMax());
                                    intent.putExtra("wechatnum", list.get(i).getWxunid());
                                    intent.putExtra("num", list.get(i).getNum()+"");
                                    intent.putExtra("sum", list.get(i).getSum()+"");
                                    if (list.get(i).getSex().contains("男")){
                                        intent.putExtra("sex", 1);
                                    }else if (list.get(i).getSex().contains("女")){
                                        intent.putExtra("sex", 2);
                                    }
                                    startActivity(intent);
                                }
                            }
                        });

                        Glide.with(getContext()).load(Path.img(wechat.getBanner().getBanner())).apply(new RequestOptions().error(R.mipmap.banner_follow).diskCacheStrategy(DiskCacheStrategy.NONE)).into(banner);
                        banner.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (UserBean.uid == 0) {
                                    startActivity(new Intent(getContext(), LoginActivity.class));
                                } else {
                                    Intent intent = new Intent(getActivity(), FollowActivity.class);
                                    intent.putExtra("iv", wechat.getBanner().getHead());
                                    intent.putExtra("title", wechat.getBanner().getName());
                                    intent.putExtra("one", wechat.getBanner().getDan());
                                    intent.putExtra("all", wechat.getBanner().getMax());
                                    intent.putExtra("wechatnum", wechat.getBanner().getWxunid());
                                    intent.putExtra("num", wechat.getBanner().getNum()+"");
                                    intent.putExtra("sum", wechat.getBanner().getSum()+"");
                                    startActivity(intent);
                                }
                            }
                        });
                        loadService.showSuccess();
                    } catch (Exception e) {
                        lv.setVisibility(View.GONE);
                        loadService.showCallback(ErrorCallback.class);
                    }
                } else {
                    loadService.showCallback(ErrorCallback.class);
                }
            }
        });
    }
}
